<?php
ob_start();
session_start();
include "checksession.php";
date_default_timezone_set('Asia/Bangkok');
include("../connect.php");
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$strSQL = "SELECT * FROM product INNER JOIN product_detail ON product.prd_id = product_detail.prd_id where product.prd_id = '".$_GET["id"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$prd_id = $objResult["prd_id"];
if(!$objResult)
{
	echo "Not found id=".$_GET["id"];
}
else
{
?>
<html>
<head>
<title>Adinop Co., Ltd. was founded as an ingredients importer and local manufacturers’ distributor in 1986. We also function as an exporter of finished products</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="../css/style.css"/>
<link type="text/css" href="../css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script src="../js/jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../js/jquery/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript" src="../js/jquery/timepicker.js"></script>
</head>

<script language="javascript">
			function CheckValidate()
			{
			if(eval("document.frmMain.txttypename.value")==""){
				alert("กรุณากรอกช่อง Type Name");
				eval("document.frmMain.txttypename.focus();")
				return false;
				}
			}
</script>
<script type="text/javascript">
        $(function() {         
            $('#stDateTime').datepicker({
                duration: '',
                dateFormat: "yy-mm-dd",
                constrainInput: false
            });
                 $('#enDateTime').datepicker({
                duration: '',
                dateFormat: "yy-mm-dd",
                constrainInput: false
            });
        });
    </script>

</script>
<body>
   <body class="body">

            <table align="center" width="90%">
                <tr>
                    <td colspan="2" class="header" >
                      <img src="../images/logo.png"> แก้ไขรายละเอียดอุปกรณ์ 
                        <hr/>
                    </td>
                </tr >
              </table>
<br/>
<div align="center">
 <fieldset>
<legend class="nameTitle">รายละเอียดข้อมูล</legend>
<form action="<? $editFormAction;?>" method="post" name ="frmMain" id="frmMain" ENCTYPE="multipart/form-data" OnSubmit="return CheckValidate();" />
<table width="100%" border="0" class="inputcontenttable">
  <tr>
    <th width="50%">
      <div align="right">Product ID : </div></th>
    <th><div align="left">
      <input type="text" name="txtprd_id" id="txtprd_id" size="20" value="<?=$objResult["prd_id"];?>" maxlength= "8" readonly="" />
  </tr>

   <tr>
    <th width="50%">
      <div align="right">Product Name : </div></th>
    <th><div align="left">
      <input type="text" name="txtprd_name" id="txtprd_name" size="20" value="<?=$objResult["prd_name"];?>"/>
  </tr>
	<img src="photo/<?=$objResult["prd_pic"];?>" width="100" height="100">
   <tr>
    <th width="50%">
      <div align="right">Pic : </div></th>
    <th><div align="left">
      <input type="file" name="fileupload" id="fileupload" size="32" />
  </tr>
   <tr>
                        <th width="40%"> 
                    <div align="right">เริ่มประกัน : </div></th>
                    <th width="236"><div align="left">
                        <input type="text" name="stDateTime" id="stDateTime" value="<?=$objResult["pd_stWarunty"] ?>" size="20" />
                    </div></th>
                    </tr>
 <tr>
                        <th width="40%"> 
                    <div align="right">สิ้นสุดประกัน : </div></th>
                    <th width="236"><div align="left">
                        <input type="text" name="enDateTime" id="enDateTime" value="<?=$objResult["pd_enWarunty"] ?>" size="20" />
                    </div></th>
                    </tr>
</table>
<br/>
 </fieldset>
<br/>
<input name="btnProductedit" type="submit" id="btnProductedit" value="แก้ไข" style="height: 30px; width: 120px" />
</form>
</div>
<br/>
</fieldset>


</body>
  <? 
	  if(isset($_POST['btnProductedit'])){
	  $txtprd_id = $_POST['txtprd_id'];
	  $txtprd_name = $_POST['txtprd_name'];
          $stDateTime = $_POST['stDateTime'];
          $enDateTime = $_POST['enDateTime'];		
	  $pic = $_FILES['fileupload']['name'];
	  $pic = iconv("tis-620", "utf-8",$pic);
	  uppic_only($_FILES["fileupload"],"photo/");
          

	if($pic==""){
		$sql1 = "update product set prd_name= '$txtprd_name' where prd_id ='$txtprd_id' ";
		$rsUpdate=mysql_query($sql1);

		$sql2 = "update product_detail set pd_name= '$txtprd_name' , `pd_stWarunty`  = '$stDateTime', `pd_enWarunty` = '$enDateTime' where prd_id ='$txtprd_id' ";
		$rsUpdate2=mysql_query($sql2);

	}else{
		$sql1 = "update product set prd_name= '$txtprd_name' , prd_pic= '$pic' where prd_id ='$txtprd_id' ";
		$rsUpdate=mysql_query($sql1);

		$sql2 = "update product_detail set pd_name= '$txtprd_name' where prd_id ='$txtprd_id' ";
		$rsUpdate2=mysql_query($sql2);
	}
	   if($rsUpdate){
		echo "<script language='javascript'>
			   alert('Update Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
		 echo "<meta http-equiv='refresh' content='1; url=manageTreasury.php'>";
		}
	}
}


  mysql_close($connection);
  ?>
<?php
function uppic_only($img,$imglocate,$limit_size=2000000,$limit_width=0,$limit_height=0){
	$allowed_types=array("jpg","jpeg","gif","png");   
//	echo "1<br>";
	if($img["name"]!=""){
		$fileupload1=$img["tmp_name"];
		$data_Img=@getimagesize($fileupload1);
		$g_img=explode(".",$img["name"]);
		$ext = strtolower(array_pop($g_img));  
		$file_up=$_FILES['fileupload']['name'].$i_num.".".$ext;		
		$canUpload=0;
//		echo "2<br>";
		if(isset($data_Img) && $data_Img[0]>0 && $data_Img[1]>0){
//			echo "3<br>";
			if($img["size"]<=$limit_size){				
				if($limit_width>0 && $limit_height>0){
					if($data_Img[0]<=$limit_width && $data_Img[1]<=$limit_height){
						$canUpload=1;
						echo "5<br>";

					}					
				}else if($limit_width>0 && $limit_height==0){
					if($data_Img[0]<=$limit_width){
						$canUpload=1;
						echo "6<br>";
					}		
				}else if($limit_width==0 && $limit_height>0){
					if($data_Img[1]<=$limit_height){
						$canUpload=1;
						echo "7<br>";
					}												
				}else{
					$canUpload=1;					
//					echo "8<br>";
				}			
			}else{
				echo "<script language='javascript'>
		  $(document).ready(function(){
			apprise('â»Ã´ãÊèä¿ÅìÃÙ»¢¹Ò´äÁèà¡Ô¹ 2 MB', {'animate':true}, function (){
				});
			})
		  </script>";
			exit();
			}			
		}		
		if($fileupload1!="" && @in_array($ext,$allowed_types) && $canUpload==1){			
				@copy($fileupload1,$imglocate.$file_up);	
				@chmod($imglocate.$file_up,0777);								
		}else{
			$file_up="";
		}
	}
	return $file_up; // Êè§¡ÅÑºª×èÍä¿Åì
}
?>
</html>